package com.example.demo.base;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class QueryBuilder<T> implements Serializable{

    private List<Filter> filters=new ArrayList<>();

    private List<Order> orders=new ArrayList<>();

    protected T entity;

    /**false:查询deleted=0,true:查询所有*/
    private boolean deleted;


    public QueryBuilder(){
       this(false);
    }

    public QueryBuilder(boolean deleted){
        this(null,deleted);
    }

    public QueryBuilder(T entity){
        this(entity,false);
    }

    public QueryBuilder(T entity,boolean deleted){
        this.entity=entity;
        this.deleted=deleted;
    }

    public List<Filter> getFilters() {
        return filters;
    }

    public void setFilters(List<Filter> filters) {
        this.filters = filters;
    }

    public List<Order> getOrders() {
        return orders;
    }

    public void setOrders(List<Order> orders) {
        this.orders = orders;
    }

    public QueryBuilder addFilter(Filter filter){
        filters.add(filter);
        return this;
    }

    public QueryBuilder addAndNewFilter(Filter filter){
        filters.add(new Filter(null, Filter.Operator.andNew, null));
        filters.add(filter);
        return this;
    }

    public QueryBuilder addOrNewFilter(Filter filter){
        filters.add(new Filter(null, Filter.Operator.orNew, null));
        filters.add(filter);
        return this;
    }

    public QueryBuilder addOrFilter(Filter filter){
        filters.add(new Filter(null, Filter.Operator.or, null));
        filters.add(filter);
        return this;
    }

    public QueryBuilder addOrder(Order order){
        orders.add(order);
        return this;
    }

    public T getEntity() {
        return entity;
    }

    public void setEntity(T entity) {
        this.entity = entity;
    }

    public boolean isDeleted() {
        return deleted;
    }

    public void setDeleted(boolean deleted) {
        this.deleted = deleted;
    }

    public boolean clear(){
        try {
            filters.clear();
            orders.clear();
            deleted = false;
            this.entity=null;
            return true;
        }catch (Exception e){

        }
        return false;
    }
}
